<template>
<div id="app">
  <!--  动态面包屑-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item v-for="breadCrumbItem in breadCrumbList" :key="breadCrumbList.path">
      {{breadCrumbItem.meta.title}}
    </el-breadcrumb-item>
  </el-breadcrumb>

  <el-card>

        <el-button type="primary" icon="el-icon-plus" round @click="rolesDialogVisible = true">添加角色</el-button>


      <el-table
          :data="roleList"
          stripe
          border
          style="width: 100%"
          lazy>
        <el-table-column  type="expand"  ></el-table-column>
        <el-table-column  type="index" label="#" ></el-table-column>
        <el-table-column prop="roleName" label="角色名称" ></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" ></el-table-column>
        <el-table-column  label="操作" width="300px">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-search">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-search">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-search">分配权限</el-button>

          </template>
        </el-table-column>
      </el-table>


  </el-card>
  <el-dialog
      title="添加角色"
      :visible.sync="rolesDialogVisible"
      width="30%"
      @close="rolesDialogVisible">
    <el-form :model="addRolesForm" :rules="rolesFormRules" ref="addRolesFormRef" label-width="70px" >
      <el-form-item label="角色名称" >
        <el-input v-model="addRolesForm.roleName" ></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="email">
        <el-input v-model="addRolesForm.roleDesc" ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="rolesDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addRoles">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "Roles",
  data(){
    return{
      rolesDialogVisible:false,
      roleList:[],
      addRolesForm:{
        roleName:'',
        roleDesc:''
      },
      rolesFormRules:{
        roleName:[
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        roleDesc:[
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }

    }
  },
  methods:{
    async getRolesList(){
     const{data:res}= await this.$http.get('/roles')
      if(res.meta.status!==200) return this.$message.error('获取角色列表失败！')
      this.$message.success('获取角色列表成功')
      this.roleList=res.data
      console.log(this.roleList)
    },
    addRoles(){
        this.$refs.addRolesFormRef.validate(async valid=>{
          if(!valid) return
          const {data: res} =await this.$http.post('/roles', this.addRolesForm)
          if (res.meta.status !== 201) {
            return  this.$notify({
              title: '注意！添加用户失败',
              type: 'warning',
              offset: 800
            })
          }
          this.$notify({
            title: '添加用户成功',
            type: 'success',
            offset: 800
          })
          this.rolesDialogVisible=false
          this.getRolesList()
        })
      }
  },
  created() {
    this.getRolesList()
  },
  computed:{
    //路由信息列表
    breadCrumbList(){
      console.log(this.$route)
      return  this.$route.matched
    },
  }
}
</script>

<style lang="less" scoped>
.el-card {
  box-shadow: 4px 4px 10px #cecece !important;
  margin-top: 20px;
}
.el-table{
  margin-top: 20px
}
#app{
  opacity: 0;
  animation: bottomBtn 0.5s linear 100ms forwards;
}
@keyframes bottomBtn {
  to{
    opacity: 1;
  }
  from{
    opacity: 0;
  }
}
</style>
